<template>
  <el-container>
    <el-main style="padding: 0 20px 20px 20px">
      <div class="top-tips">满足以下条件时进入当前分支</div>
      <template v-for="(conditionGroup, conditionGroupIdx) in conditionList">
        <div
            class="or-branch-link-tip"
            v-if="conditionGroupIdx != 0">
          或满足
        </div>
        <div class="condition-group-editor">
          <div class="header">
            <span>条件组 {{ conditionGroupIdx + 1 }}</span>
            <div @click="deleteConditionGroup(conditionGroupIdx)">
              <el-icon class="branch-delete-icon"><ele-Delete /></el-icon>
            </div>
          </div>

          <div class="main-content">
            <!-- 单个条件 -->
            <div class="condition-content-box cell-box">
              <div>描述</div>
              <div>条件字段</div>
              <div>运算符</div>
              <div>值</div>
            </div>
            <div
                class="condition-content"
                v-for="(condition, idx) in conditionGroup">
              <div class="condition-relation">
                <span>{{ idx == 0 ? '当' : '且' }}</span>
                <div @click="deleteConditionList(conditionGroup, idx)">
                  <el-icon class="branch-delete-icon"><ele-Delete /></el-icon>
                </div>
              </div>
              <div class="condition-content">
                <div class="condition-content-box">
                  <el-input
                      v-model="condition.label"
                      placeholder="描述" />
                  <el-input
                      v-model="condition.field"
                      placeholder="条件字段" />
                  <el-select
                      v-model="condition.operator"
                      placeholder="请选择">
                    <el-option
                        label="等于"
                        value="=="></el-option>
                    <el-option
                        label="不等于"
                        value="!="></el-option>
                    <el-option
                        label="大于"
                        value=">"></el-option>
                    <el-option
                        label="大于等于"
                        value=">="></el-option>
                    <el-option
                        label="小于"
                        value="<"></el-option>
                    <el-option
                        label="小于等于"
                        value="<="></el-option>
                    <el-option
                        label="包含"
                        value="include"></el-option>
                    <el-option
                        label="不包含"
                        value="notInclude"></el-option>
                  </el-select>
                  <el-input
                      v-model="condition.value"
                      placeholder="值" />
                </div>
              </div>
            </div>
          </div>
          <div class="sub-content">
            <el-button
                link
                type="primary"
                @click="addConditionList(conditionGroup)">
              <el-icon><ele-Plus /></el-icon>
              添加条件
            </el-button>
          </div>
        </div>
      </template>
      <el-button
          style="width: 100%"
          type="info"
          text
          bg
          @click="addConditionGroup">
        <el-icon><ele-Plus /></el-icon>
        添加条件组
      </el-button>
    </el-main>
  </el-container>
</template>
<script lang="ts" setup>
import {NodeConditionData} from "/@/components/gFlow/propertySetting/model";
import {onMounted, ref, watch} from "vue";
defineOptions({ name: "Branch"})
const emit = defineEmits(['setNodeExt'])
const conditionList = ref<Array<NodeConditionData[]>>([])
const initNodeData = (data:Array<NodeConditionData[]>)=>{
  conditionList.value = data
}
const addConditionGroup = () => {
  addConditionList(conditionList.value[conditionList.value.push([] as never) - 1])
}
const addConditionList = (conditionList:NodeConditionData[]) => {
  conditionList.push({
    label: '',
    field: '',
    operator: '',
    value: '',
  })
}
const deleteConditionGroup = (idx:number) => {
  conditionList.value.splice(idx, 1)
}
const deleteConditionList = (conditionList:NodeConditionData[],idx:number) => {
  conditionList.splice(idx,1)
}
const setNodeExt = ()=>{
  emit('setNodeExt',conditionList.value)
}
defineExpose({initNodeData,setNodeExt})
</script>

<style scoped lang="scss">
.top-tips {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  color: #646a73;
}

.or-branch-link-tip {
  margin: 10px 0;
  color: #646a73;
}

.condition-group-editor {
  user-select: none;
  border-radius: 4px;
  border: 1px solid #e4e5e7;
  position: relative;
  margin-bottom: 16px;

  .branch-delete-icon {
    font-size: 18px;
  }

  .header {
    background-color: #f4f6f8;
    padding: 0 12px;
    font-size: 14px;
    color: #171e31;
    height: 36px;
    display: flex;
    align-items: center;

    span {
      flex: 1;
    }
  }

  .main-content {
    padding: 0 12px;

    .condition-relation {
      color: #9ca2a9;
      display: flex;
      align-items: center;
      height: 36px;
      display: flex;
      justify-content: space-between;
      padding: 0 2px;
    }

    .condition-content-box {
      display: flex;
      justify-content: space-between;
      align-items: center;

      div {
        width: 100%;
        min-width: 120px;
      }

      div:not(:first-child) {
        margin-left: 16px;
      }
    }

    .cell-box {
      div {
        padding: 16px 0;
        width: 100%;
        min-width: 120px;
        color: #909399;
        font-size: 14px;
        font-weight: 600;
        text-align: center;
      }
    }

    .condition-content {
      display: flex;
      flex-direction: column;

      :deep(.el-input__wrapper) {
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
      }

      .content {
        flex: 1;
        padding: 0 0 4px 0;
        display: flex;
        align-items: center;
        min-height: 31.6px;
        flex-wrap: wrap;
      }
    }
  }

  .sub-content {
    padding: 12px;
  }
}
</style>
